package com.guodi.model.utils;

import com.guodi.common.utils.UuidUtil;
import com.guodi.core.minio.util.MinioUtil;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Base64;

public class ImageUtil {

    /**
     * MultipartFile [图片]转BASE64字符串
     * @param file
     * @return
     */
    public static String multipartFileToBASE64(MultipartFile file) throws Exception{
        BASE64Encoder bEncoder=new BASE64Encoder();
        String[] suffixArra= file.getOriginalFilename().split("\\.");
        String preffix="data:image/jpg;base64,".replace("jpg", suffixArra[suffixArra.length - 1]);
        String base64EncoderImg=preffix + bEncoder.encode(file.getBytes()).replaceAll("[\\s*\t\n\r]", "");
        return base64EncoderImg;
    }


    /**
     * 图片转Base64字符串
     * @param imageFileName
     * @return
     */
    public static String convertImageToBase64Str(String imageFileName) {
        ByteArrayOutputStream baos = null;
        try {
            //获取图片类型
            String suffix = imageFileName.substring(imageFileName.lastIndexOf(".") + 1);
            //构建文件
            File imageFile = new File(imageFileName);
            //通过ImageIO把文件读取成BufferedImage对象
            BufferedImage bufferedImage = ImageIO.read(imageFile);
            //构建字节数组输出流
            baos = new ByteArrayOutputStream();
            //写入流
            ImageIO.write(bufferedImage, suffix, baos);
            //通过字节数组流获取字节数组
            byte[] bytes = baos.toByteArray();
            //获取JDK8里的编码器Base64.Encoder转为base64字符
            return Base64.getEncoder().encodeToString(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    /**
     * Base64字符串转图片
     * @param base64String
     * @param imageFileName
     */
    public static void convertBase64StrToImage(String base64String, String imageFileName) {
        ByteArrayInputStream bais = null;
        try {
            //获取图片类型
            String suffix = imageFileName.substring(imageFileName.lastIndexOf(".") + 1);
            //获取JDK8里的解码器Base64.Decoder,将base64字符串转为字节数组
            byte[] bytes = Base64.getDecoder().decode(base64String);
            //构建字节数组输入流
            bais = new ByteArrayInputStream(bytes);
            //通过ImageIO把字节数组输入流转为BufferedImage
            BufferedImage bufferedImage = ImageIO.read(bais);
            //构建文件
            File imageFile = new File(imageFileName);
            //写入生成文件
            ImageIO.write(bufferedImage, suffix, imageFile);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bais != null) {
                    bais.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


//    public static void main(String[] args) throws FileNotFoundException {
//
//        System.out.println("----------------------------图片转Base64字符串---------------------------");
//        //图片文件路径
//        String imageFileName = "D:\\myFile\\tempFile\\minio.jpg";
//        //图片转Base64字符串
//        String base64Str = ImageUtil.convertImageToBase64Str(imageFileName);
//        System.out.println(base64Str);
//
//        System.out.println("----------------------------Base64字符串转图片---------------------------");
//        //新文件路径
//        String newFileName = "E:/app/tempFiles/csdn-new.jpg";
//        //String newFileName = "D:\\myFile\\tempFile\\imageToBase64\\csdn-new.jpg";
//        //Base64字符串转图片
//        ImageUtil.convertBase64StrToImage(base64Str, newFileName);
//        System.out.println("生成的文件的路径是："+newFileName);
//
//        //新建
//        InputStream inputStream = new FileInputStream(newFileName);
//        String uuid = UuidUtil.get32UUID();
//        String jpgUrl = MinioUtil.uploadFile(inputStream,"测试文件目录/" + uuid, "生态宜居.jpg");
//        System.out.println(jpgUrl);
//    }

}
